#include "Fungi.h"
#include "Turtle.h"
#include "MainGameScreen.h"

Fungi::Fungi(D3DXVECTOR2 position,EState state) : GameObject(position,EObjectType::EFungi,state)
{
	m_pSprite = new CSprite(ResourceManager::GetIns()->GetTexture(EResource::Fungi_ID),3,1,3,0,2);
	m_velocity = D3DXVECTOR2(FUNGI_SPEED,0);
	m_effect = SpriteEffect::None;
	m_direction=EDirection::Left;

	//new 
	m_isFall=true;
	_timeBeforeDie=0;
	m_pSprite->m_Start=0;
	m_pSprite->m_End=1;
}

Fungi::~Fungi()
{
	GameObject::~GameObject();
}

void Fungi::Update(float dt)
{
	//if(canUpdate==true)
	{
		m_pSprite->Update(dt);
		m_position += m_velocity*dt*60;

		if(m_position.y < -50)
			m_state = Die;
		if(m_state==EState::Hit)
		{
			this->m_pSprite->m_Start=2;
			this->m_pSprite->SetIndex(2);
			this->m_pSprite->m_End=2;
			_timeBeforeDie+=dt;
			if(_timeBeforeDie>0.7f)
			{
				_timeBeforeDie=0;
				m_state = EState::Die;
			}
			m_velocity.x = 0;
			m_direction = EDirection::NONE;
		}
		if(m_state == EState::KickUp)
		{
			if(rotate < 180)
				rotate += 15;
			else
				m_isFall = true;
			_timeBeforeDie+=dt;
			if(_timeBeforeDie > 2)
			{
				_timeBeforeDie = 0;
				m_state = EState::Die;
			}
		}
		if(m_isFall==true)
		{
			m_position.y+=m_velocity.y*dt*60;
			m_velocity.y -= 0.5f;
		}	
		if(m_isFall == false)
		{
			m_velocity.y = 0;
			if(m_direction == EDirection::Right)
			{
				m_effect = SpriteEffect::None;
				m_velocity.x = abs(m_velocity.x);
			}
			if(m_direction == EDirection::Left)
			{
				m_effect = SpriteEffect::Horizontally;
				m_velocity.x = -abs(m_velocity.x);
			}
		}
	}
}

void Fungi::Draw()
{
	if(m_state!=EState::Die)
		m_pSprite->Draw(m_position,SpriteEffect::None,rotate,1);
}

void Fungi::UpdateCollision(list<GameObject*> listObjects, float dt)
{
	float timeTmp = 0;
	list<GameObject*>::iterator it;
	float moveX,moveY;	
	float normalx,normaly;
	float timeCols = 0;
	Box tmpBox = this->GetBox();
	Box tmpBox2 = tmpBox;
	tmpBox2.y -= 2;
	bool tmpIsFall = true;
	if(this->GetObjectState()==EState::KickUp)
	{
		return;
	}
	for(it=listObjects.begin(); it!=listObjects.end(); ++it)
	{

		if((*it) == this)
			continue;
		GameObject* otherObject = (*it);

		//neu doi tuong da chet thi bo qua
		if(otherObject->GetObjectState()==EState::Die || otherObject->GetObjectState()==EState::Breaking)
			continue;

		EObjectType typeOther=otherObject->GetObjectType();
		Box otherBox = otherObject->GetBox();

		if(otherObject->GetKindOfObject()==ESolid)
		{
			if(AABB(tmpBox2,otherObject->GetBox(),moveX,moveY) == true)
			{	
				if(moveY !=0)
				{
					tmpIsFall = false;
				}
			}
		}

		if(AABB(tmpBox,otherBox,moveX,moveY) == false)
		{
			if(otherObject->GetKindOfObject()==EDynamic)
			{
				tmpBox.vx -= otherBox.vx;				
				tmpBox.vy -= otherBox.vy;
				otherBox.vx = 0;
				otherBox.vy = 0;
			}
			else
			{
				tmpBox = this->GetBox();
			}

			if(AABBCheck(GetSweptBroadphaseBox(tmpBox),otherBox)==true || AABBCheck(GetSweptBroadphaseBox(otherBox),tmpBox)==true)
			{
				
				timeCols = SweptAABB(tmpBox,otherBox,normalx,normaly);
				if(timeCols > 0 && timeCols < 1)
				{
					timeCols*=dt;
					ECollisionDirect collDriect = GetCollisionDirect(normalx,normaly);
					float remainingtime = dt - timeCols;

					//////////////////////////////---Land---//////////////////////////////////

					if(otherObject->GetKindOfObject()==ESolid)
					{
						switch(collDriect)
						{
						case Colls_Bot:
							{
								m_position.y += m_velocity.y*timeCols*60 + 1;
								tmpIsFall = false;
								m_velocity.y = 0;
								m_isFall = false;
							}
							break;
						case Colls_Top:
							{
								m_position.y += m_velocity.y*timeCols*60;
								m_velocity.y = -m_velocity.y/2;
							}
							break;
						case Colls_Left:
							{
								m_position.x += m_velocity.x*timeCols*60 + 1;
								m_direction = EDirection::Right;
							}
							break;
						case Colls_Right:
							{
								m_position.x += m_velocity.x*timeCols*60 - 1;
								m_direction = EDirection::Left;
							}
							break;
						}
					}					

					///////////////////////////----Enemy----//////////////////////
					if(typeOther == EFungi)
					{
						switch(collDriect)
						{
						case Colls_Bot:
							{
								m_position.y += m_velocity.y*timeCols*60 + 1;
								m_velocity.y = 0;
								tmpIsFall = false;
								m_isFall = false;
							}
							break;
						case Colls_Top:
							break;
						case Colls_Left:
							{
								m_position.x += m_velocity.x*timeCols*60 + 1;
								m_direction = EDirection::Right;
							}
							break;
						case Colls_Right:
							{
								m_position.x += m_velocity.x*timeCols*60 - 1;
								m_direction = EDirection::Left;
							}
							break;
						}
					}

					if(typeOther == ETurtle)
					{
						Turtle* tmpTurtle = (Turtle*)otherObject;
						if(otherObject->GetObjectState()==EState::Hit && otherObject->GetVelocity().x!=0)
						{
							SoundManager::GetInst()->PlaySoundEffect(ES_KickUp);
							m_velocity.y = 3;									
							m_state = EState::KickUp;
							MainGameScreen::score+=100;
						}
						switch(collDriect)
						{
						case Colls_Bot:
							{
								m_position.y += m_velocity.y*timeCols*60 + 1;
								m_velocity.y = 0;
								tmpIsFall = false;
								m_isFall = false;
							}
							break;
						case Colls_Top:
							break;
						case Colls_Left:
							{
								m_position.x += m_velocity.x*timeCols*60 + 1;
								m_direction = EDirection::Right;								
							}
							break;
						case Colls_Right:
							{	
								m_position.x += m_velocity.x*timeCols*60 - 1;
								m_direction = EDirection::Left;															
							}
							break;
						}
					}
				}
				else //not collision sweap AABB
				{

				}
			}
		}
		else
		{
			if(otherObject->GetKindOfObject()==ESolid)
			{
				if(moveX<0)
				{
					m_position.x += moveX - 1;
					m_direction = EDirection::Left;
				}
				if( moveX > 0)
				{					
					m_position.x += moveX + 1;
					m_direction = EDirection::Right;
				}
				if(moveY !=0)
				{
					m_position.y += moveY+1;
					tmpIsFall = false;
					m_isFall = false;
					m_velocity.y = 0;
					if(typeOther==EBreakBrick && otherObject->GetVelocity().y > 0)
					{
						m_state = KickUp;
						MainGameScreen::score+=100;
					}
				}
			}

			if(typeOther == EFungi)
			{
				if(moveX < 0)
				{
					m_position.x += moveX - 1;
					m_direction = EDirection::Left;
				}
				else if(moveX > 0)
				{
					m_position.x += moveX + 1;
					m_direction = EDirection::Right;
				}
			}

			if(typeOther == ETurtle)
			{
				Turtle* tmpTurtle = (Turtle*)otherObject;
				if(otherObject->GetObjectState()==EState::Hit && otherObject->GetVelocity().x !=0)
				{
					SoundManager::GetInst()->PlaySoundEffect(ES_KickUp);
					m_velocity.y = 5;									
					m_state = EState::KickUp;
					MainGameScreen::score+=100;
				}
				if(moveX < 0)
				{
					m_position.x += moveX - 1;
					m_direction = EDirection::Left;
				}
				else if(moveX > 0)
				{
					m_position.x += moveX + 1;
					m_direction = EDirection::Right;
				}
			}
		}
	}
	if(tmpIsFall==true)
		SetFall(true);
}
